﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Smart.Common;

namespace Smart.Util.Entities
{
	public class GenerateSharePoint : CommandBase
	{
		public struct ContentTypeInfo
		{
			public string Id;
			public string Name;

			public ContentTypeInfo(string id, string name)
			{
				Id = id;
				Name = name;
			}

			public ContentTypeInfo(params string[] args)
			{
				if (args == null || args.Length <= 0)
					throw new InvalidDataException("Incorrect ContentTypeInfoFile");

				Id = args[0];
				Name = args.Length > 1 ? args[1] : null;
			}
		}

		public GenerateSharePoint(CommandBase parent, string[] args, int index)
			: base("SharePoint", parent, args, index)
		{
		}

		protected override void RegisterSubCommands()
		{
		}

		protected override bool ExecuteInternal()
		{
			if (!CheckArguments(3, @"SiteUrl NameSpace ContentTypesInfoFile"))
				return false;

			var siteUrl = Args[Index];
			var ns = Args[Index + 1];
			var ctf = Args[Index + 2];

			var cts =
				File
					.ReadAllLines(ctf, Encoding.UTF8)
					.Where(s => !s.IsNullOrEmpty())
					.Select(s => new ContentTypeInfo(s.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries)))
					.ToList();

			var dcts =
				cts.ToDictionary(info => info.Id, StringComparer.OrdinalIgnoreCase);

			using (var site = new SPSite(siteUrl))
			using (var web = site.OpenWeb())
			{
				var contentTypes = web.ContentTypes;

				for (int i = 0, iMax = cts.Count; i < iMax; i++)
				{
					var info = cts[i];

					var contentType = contentTypes[new SPContentTypeId(info.Id)];

					;
				}
			}

			throw new NotImplementedException();
		}
	}
}